<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQF" inline>
        <el-form-item label="分类编码">
          <el-input v-model="listQF.keyword" placeholder="物料编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="ResetList">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button
        type="success"
        :disabled="ids.length != 1"
        @click="OpenAddDialogU"
        >新增</el-button
      >
      <el-button @click="handleDel(true)">删除</el-button>
      <el-table :data="list" @selection-change="selectionChange">
        <el-table-column type="selection"></el-table-column>
        <el-table-column
          v-for="item in listColumn"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="操作" v-slot="{ row }" width="280px">
          <div>
            <el-button size="mini" @click="OpenAddDialog(row)">新增</el-button>
            <el-button type="primary" size="mini" @click="OpenEditDialog(row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(false, row.id)"
              >删除</el-button
            >
          </div>
        </el-table-column>
      </el-table>
      <el-pagination
        style="display: flex; justify-content: right"
        @size-change="PagerLimitChange"
        @current-change="PagerPageChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 30, 40]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      center
      :visible="visible"
      :title="title"
      @close="close"
      :close-on-click-modal="false"
    >
      <el-form inline ref="listRef" :model="listEF">
        <el-form-item label="父级分类" prop="pid">
          <el-cascader
            v-model="listEF.pid"
            :options="options"
            :disabled="isDisable"
            :props="{
              checkStrictly: true,
              label: 'name',
              value: 'id',
              emitPath: false,
            }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="分类编号" prop="code" required>
          <el-input v-model="listEF.code" placeholder="分类编号"></el-input>
        </el-form-item>
        <el-form-item label="分类名称 " prop="name" required>
          <el-input v-model="listEF.name" placeholder="分类名称"></el-input>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input
            type="textarea"
            v-model="listEF.remark"
            placeholder="说明"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="handlesubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getMaterialCatgoryList, getMaterialCatgoryChildrenList, DelMaterialCategory, addMaterialCatgory, editMaterialCatgory } from '@/api_zc/wms/material-category'
export default {
  mounted () {
    this.getList()
    this.getUnity()
  },
  data () {
    return {
      options: [],
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      list: [],
      listColumn: [
        {
          label: 'id',
          prop: 'id'
        },
        {
          label: '父级分类id',
          prop: 'pid'
        },
        {
          label: '分类编码',
          prop: 'code'
        },
        {
          label: '分类名称',
          prop: 'name'
        },
        {
          label: '说明',
          prop: 'remark'
        },
      ],
      listQF: {
        keyword: '',
      },
      listEF: {
        id: '',
        code: '',
        name: '',
        remark: '',
        pid: '',
      },
      visible: false,
      title: '修改销售退货',
      ids: [],
      isDisable: false
    }
  },
  methods: {
    async getUnity () {
      const { data } = await getMaterialCatgoryChildrenList()
      this.options = data
    },
    async getList () {
      const { data: { total, content } } = await getMaterialCatgoryList({ ...this.listQF, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    PagerLimitChange (val) {
      this.pager.limit = val
      this.getList()
    },
    PagerPageChange (val) {
      this.pager.page = val
      this.getList()
    },
    ResetList () {
      Object.assign(this.listQF, this.$options.data().listQF)
      this.getList()
    },
    OpenEditDialog (row) {
      this.isAdd = false
      this.visible = true
      this.$nextTick(() => {
        for (let key in row) {
          if (row.hasOwnProperty(key)) {
            if (this.listEF.hasOwnProperty(key)) {
              this.$set(this.listEF, key, row[key])
            }
          }
        }
      })
      this.title = '修改销售退货'
      this.addDisable(this.options, row.id)
    },
    addDisable (list, id) {
      list.forEach(item => {
        if (item.id == id) {
          this.$set(item, 'disabled', true)
          return
        }
        if (item.children) {
          this.addDisable(item.children, id)
        }
      })
    },
    OpenAddDialog (row) {
      this.isAdd = true
      this.visible = true
      this.title = '添加销售退货'
      this.$nextTick(() => { this.listEF.pid = row.id })
      this.isDisable = false
    },
    OpenAddDialogU () {
      this.isAdd = true
      this.visible = true
      this.title = '添加销售退货'
      this.$nextTick(() => {
        this.listEF.pid = this.ids[0]
      })
      this.isDisable = false
    },
    close () {
      this.visible = false
      this.$refs.listRef.resetFields()
      this.getUnity()
    },
    handleDel (status, id) {
      let ids = []
      if (status) {
        ids = this.ids
      } else {
        ids[0] = id
      }
      this.$confirm(`是否确认删除物料分类编号为${ids.join(',')}的数据项？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await DelMaterialCategory({ ids })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectionChange (selectArray) {
      this.ids = selectArray.map(item => item.id)
    },
    handlesubmit () {
      this.$refs.listRef.validate(async (callback) => {
        if (!callback) {
          return
        }
        if (this.isAdd) {
          await addMaterialCatgory({ ...this.listEF })
          this.$message.success('新建成功')
        } else {
          await editMaterialCatgory({ ...this.listEF })
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
        this.getUnity()
        this.$refs.listRef.resetFields()
      })
    }
  }
}
</script>

<style>
</style>